package io.github.lee.repository.qb.domain

import android.content.Context
import androidx.annotation.Keep
import com.google.gson.annotations.SerializedName
import io.github.lee.repository.qb.R
import kotlin.math.roundToInt


@Keep
data class Torrent(
    @SerializedName("added_on")
    val addedOn: Int? = 0,
    @SerializedName("amount_left")
    val amountLeft: Long? = 0,
    @SerializedName("auto_tmm")
    val autoTmm: Boolean? = false,
    @SerializedName("availability")
    val availability: Double? = 0.0,
    @SerializedName("category")
    val category: String? = "",
    @SerializedName("completed")
    val completed: Long? = 0,
    @SerializedName("completion_on")
    val completionOn: Int? = 0,
    @SerializedName("content_path")
    val contentPath: String? = "",
    @SerializedName("dl_limit")
    val dlLimit: Int? = 0,
    @SerializedName("dlspeed")
    val dlspeed: Int? = 0,
    @SerializedName("downloaded")
    val downloaded: Long? = 0,
    @SerializedName("downloaded_session")
    val downloadedSession: Int? = 0,
    @SerializedName("eta")
    val eta: Long? = 0,
    @SerializedName("f_l_piece_prio")
    val fLPiecePrio: Boolean? = false,
    @SerializedName("force_start")
    val forceStart: Boolean? = false,
    @SerializedName("last_activity")
    val lastActivity: Int? = 0,
    @SerializedName("magnet_uri")
    val magnetUri: String? = "",
    @SerializedName("max_ratio")
    val maxRatio: Int? = 0,
    @SerializedName("max_seeding_time")
    val maxSeedingTime: Int? = 0,
    @SerializedName("name")
    val name: String? = "",
    @SerializedName("num_complete")
    val numComplete: Int? = 0,
    @SerializedName("num_incomplete")
    val numIncomplete: Int? = 0,
    @SerializedName("num_leechs")
    val numLeechs: Int? = 0,
    @SerializedName("num_seeds")
    val numSeeds: Int? = 0,
    @SerializedName("priority")
    val priority: Int? = 0,
    @SerializedName("progress")
    val progress: Double? = 0.0,
    @SerializedName("ratio")
    val ratio: Double? = 0.0,
    @SerializedName("ratio_limit")
    val ratioLimit: Int? = 0,
    @SerializedName("save_path")
    val savePath: String? = "",
    @SerializedName("seeding_time")
    val seedingTime: Int? = 0,
    @SerializedName("seeding_time_limit")
    val seedingTimeLimit: Int? = 0,
    @SerializedName("seen_complete")
    val seenComplete: Int? = 0,
    @SerializedName("seq_dl")
    val seqDl: Boolean? = false,
    @SerializedName("size")
    val size: Long? = 0,
    @SerializedName("state")
    val state: String? = "",
    @SerializedName("super_seeding")
    val superSeeding: Boolean? = false,
    @SerializedName("tags")
    val tags: String? = "",
    @SerializedName("time_active")
    val timeActive: Int? = 0,
    @SerializedName("total_size")
    val totalSize: Long? = 0,
    @SerializedName("tracker")
    val tracker: String? = "",
    @SerializedName("trackers_count")
    val trackersCount: Int? = 0,
    @SerializedName("up_limit")
    val upLimit: Int? = 0,
    @SerializedName("uploaded")
    val uploaded: Long? = 0,
    @SerializedName("uploaded_session")
    val uploadedSession: Int? = 0,
    @SerializedName("upspeed")
    val upspeed: Long? = 0
) {

    fun formatState(context: Context): String = when (state) {
        "error" -> context.getString(R.string.qb_state_error)
        "missingFiles" -> context.getString(R.string.qb_state_missing_files)
        "uploading" -> context.getString(R.string.qb_state_uploading)
        "pausedUP" -> context.getString(R.string.qb_state_paused_up)
        "queuedUP" -> context.getString(R.string.qb_state_queued_up)
        "stalledUP" -> context.getString(R.string.qb_state_stalled_up)
        "checkingUP" -> context.getString(R.string.qb_state_checking_up)
        "forcedUP" -> context.getString(R.string.qb_state_forced_up)
        "allocating" -> context.getString(R.string.qb_state_allocating)
        "downloading" -> context.getString(R.string.qb_state_downloading)
        "metaDL" -> context.getString(R.string.qb_state_meta_dl)
        "pausedDL" -> context.getString(R.string.qb_state_paused_dl)
        "queuedDL" -> context.getString(R.string.qb_state_queued_dl)
        "stalledDL" -> context.getString(R.string.qb_state_stalled_dl)
        "checkingDL" -> context.getString(R.string.qb_state_checking_dl)
        "forcedDL" -> context.getString(R.string.qb_state_forced_dl)
        "checkingResumeData" -> context.getString(R.string.qb_state_checking_resume_data)
        "moving" -> context.getString(R.string.qb_state_moving)
        "unknown" -> context.getString(R.string.qb_state_unknown)
        else -> context.getString(R.string.qb_state_unknown)
    }

    val formatProgress:Int
        get() = progress?.times(100)?.roundToInt() ?: 0


}
